package cn.wit.sst.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.wit.sst.domain.College;
import cn.wit.sst.domain.Course;
import cn.wit.sst.domain.Major;
import cn.wit.sst.domain.Raw;
import cn.wit.sst.domain.Score;
import cn.wit.sst.domain.Student;

public class Extractor {
	// 从原始数据中读取所有的学校
	public static List<College> extractCollege(List<Raw> raws) {
		List<College> colleges = new ArrayList<>();
		// 使用HashSet保存学校名字，保证唯一
		Set<String> names = new HashSet<>();

		for (Raw raw : raws) {
			String collegeName = raw.getCollege();

			if (!names.contains(collegeName)) {
				names.add(collegeName);

				College c = new College();
				c.setName(collegeName);
				colleges.add(c);
			}
		}

		return colleges;
	}

	// 从原始数据中读取所有的专业
	public static List<Major> extractMajor(List<Raw> raws) {
		List<Major> majors = new ArrayList<>();

		// 使用HashSet保存专业名字，保证不重复
		Set<String> clazzNames = new HashSet<>();

		for (Raw raw : raws) {
			String clazz = raw.getClazz();	// 2011信息与计算科学01
			clazz = clazz.substring(4, clazz.length() - 2);

			if (!clazzNames.contains(clazz)) {
				clazzNames.add(clazz);

				Major m = new Major();
				m.setName(clazz);
				m.setCollegeName(raw.getCollege());

				majors.add(m);
			}
		}

		return majors;
	}

	// 从原始数据中读取所有的课程
	public static List<Course> extractCourse(List<Raw> raws) {
		List<Course> courses = new ArrayList<>();

		// 使用HashSet保存课程名字，保证不重复
		Set<String> names = new HashSet<>();

		for (Raw raw : raws) {
			String course = raw.getCourseName();

			if (!names.contains(course)) {
				names.add(course);

				Course c = new Course();
				c.setName(course);

				courses.add(c);
			}
		}

		return courses;
	}

	// 从原始数据中读取所有的学生
	public static List<Student> extractStudent(List<Raw> raws) {
		List<Student> students = new ArrayList<>();

		// 使用HashSet保存学生学号，保证不重复
		Set<Long> set = new HashSet<>();

		for (Raw raw : raws) {
			Long sno = raw.getSno();

			if (!set.contains(sno)) {
				set.add(sno);

				Student s = new Student();
				s.setName(raw.getName());
				s.setSno(raw.getSno());

				// 班级为01或者02
				String clazz = raw.getClazz();	// 2011信息与计算科学01
				s.setClazz(Byte.parseByte(clazz.substring(clazz.length() - 2)));

				// 入学年份
				s.setYear(Short.parseShort(clazz.substring(0, 4)));

				// 专业
				s.setMajor(clazz.substring(4, clazz.length() - 2));

				students.add(s);
			}
		}

		return students;
	}

	// 从原始数据中读取所有的分数
	public static List<Score> extractScore(List<Raw> raws) {
		List<Score> scores = new ArrayList<>();

		for (Raw raw : raws) {
			Score s = new Score();
			s.setSno(raw.getSno());
			s.setCourse(raw.getCourseName());

			s.setCourseNo(raw.getCourseNo());
			s.setCredit(raw.getCredit());

			String grade = raw.getGrade();	// 2011-2012
			s.setStartYear(Short.valueOf(grade.substring(0, 4)));
			s.setEndYear(Short.valueOf(grade.substring(5)));

			s.setGpa(raw.getGpa());
			s.setRetakeMark(raw.getRetakeMark());

			s.setScoreEnd(raw.getScoreEnd());
			s.setScoreExp(raw.getScoreExp());
			s.setScoreMid(raw.getScoreMid());
			s.setScoreOrd(raw.getScoreOrd());
			s.setScoreRet(raw.getScoreRet());
			s.setScoreSup(raw.getScoreSup());
			s.setScoreTotal(raw.getScoreTotal());


			s.setSemester(raw.getSemester());
			s.setType("必修课".equals(raw.getCourseType()) ? (byte) 1 : (byte) 2);
			scores.add(s);
		}

		return scores;
	}
}
